//
//  FileCacheManeger.swift
//  QKYC
//
//  Created by 陈鹏飞 on 2019/2/26.
//  Copyright © 2019 zhan. All rights reserved.
//

import Foundation

open class FileCacheManeger: NSObject {
    
    public static var sharedInstance : FileCacheManeger {
        struct Static {
            static let instance : FileCacheManeger = FileCacheManeger()
        }
        return Static.instance
    }
    
    //MARK:- 获取plist文件内容
    public func readDictionaryPlistForKey(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String) -> NSMutableDictionary
    {
        let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let plistPath = searchPath[0] + "/" + fileName + ".plist"
        let dic = NSMutableDictionary(contentsOfFile:plistPath)
        
        if dic == nil
        {
            return NSMutableDictionary()
        }
        return dic!
    }
    
    //MARK:- 获取Documents目录下文件路径
    public func documentFilePath(fileName:String) -> String
    {
        // /Library/
        let path = NSHomeDirectory() + "/Documents/" + fileName
        return path
    }
    
    //MARK:- 判断文件夹是否存在
    public func fileExistAtPath(filePath:String) -> Bool
    {
        if (FileManager.default.fileExists(atPath: filePath))
        {
            return true
        }
        return false
    }
    
    //MARK:- 删除文件
    public func deleteFile(filePath:String)
    {
        if (fileExistAtPath(filePath: filePath))
        {
            let fileUrl = URL(fileURLWithPath:filePath)
            try! FileManager.default.removeItem(at: fileUrl)
        }
    }
    
    //MARK:- 创建文件夹
    public func creatFile(filePath:String)
    {
        if !FileManager.default.fileExists(atPath: filePath)
        {
            let fileUrl = URL(fileURLWithPath:filePath)
            // TODO withIntermediateDirectories 参数为true 表示文件路径不存在 也会创建
            try! FileManager.default.createDirectory(at: fileUrl, withIntermediateDirectories: true, attributes: nil)
        }
    }
    
    //MARK:- 二进制写入文件
    public func writeToFile(data:Data,fileName:String)
    {
        let filePath = documentFilePath(fileName: fileName)
        creatFile(filePath: filePath)
        let fileUrl = URL(fileURLWithPath:filePath)
        try! data.write(to: fileUrl)
    }
    
    //MARK:- 将资源文件的写入文件
    public func writeDataToFile(fileName:String,fileType:String,searchPathDirectory:FileManager.SearchPathDirectory)
    {
        let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let filePath = searchPath[0] + "/" + fileName + "." + fileType
        
        if  !FileManager.default.fileExists(atPath: filePath)
        {
            let defaultFilePath = Bundle.main.path(forResource: fileName, ofType: fileType)
            try! FileManager.default.copyItem(atPath: defaultFilePath!, toPath: filePath)
        }
    }
    
    //MARK:- 创建或向已存在的plist文件写数据
    public func updatePlist(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,infoDic:NSDictionary)
    {
        let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let plistPath = searchPath[0] + "/" + fileName + ".plist"
        if !FileManager.default.fileExists(atPath: plistPath)
        {
            FileManager.default.createFile(atPath: plistPath, contents: nil, attributes: nil)
        }
        infoDic.write(toFile: plistPath, atomically: true)
    }
    
    //MARK:- 获取plist文件根目录某个字段
    public func readPlistForKey(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,key:String) -> String
    {
        let dic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: searchPathDirectory, fileName: fileName)
        let keyString = dic.object(forKey: key) as! String
        return keyString
    }
    
    //MARK:- 更新plist文件根目录某个字段对应的数据
    public func updatePlist(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,key:String,object:Any)
    {
        let dic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: searchPathDirectory, fileName: fileName)
        dic.setValue(object, forKey: key)
        updatePlist(searchPathDirectory: searchPathDirectory, fileName: fileName, infoDic: dic)
    }
    
    
    
    public let USER_INFO          = "userInfo"
    public let USER_SETTING_INFO  = "userSettingInfo"
    
    // localUserInfo.plist
    public let USER_INFO_LOCAL    = "localUserInfo"
    
    // MARK: - 保存登录信息，实际上是存在LocalUserInfo.plist文件里面，infoKey是对应Key
    public func saveInfoToLocal(info:Any , infoKey:String)
    {
        var userInfoDic = NSMutableDictionary()
        userInfoDic = readDictionaryPlistForKey(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL)
        userInfoDic.setValue(info, forKey: infoKey)
        updatePlist(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL, infoDic: userInfoDic)
    }
    
    //MARK: - 根据key来查找存放在LocalUserInfo.plist文件对应的数据
    public func infoFromLocal(infoKey:String) -> Any
    {
        let userInfoDic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL)
        let object = userInfoDic.object(forKey: infoKey) as Any
        return object
    }
    
    //MARK: - 保存登录时的用户信息
    public func saveUserInfoToLocal(_ userInfoDic : NSDictionary)
    {
        saveInfoToLocal(info: userInfoDic, infoKey: USER_INFO)
    }
    
    //MARK: - 返回保留的登录信息
    public func userInfoFromLocal() -> NSDictionary
    {
        let userInfoDic : NSDictionary = infoFromLocal(infoKey: USER_INFO) as! NSDictionary
        return userInfoDic
    }
    
    //MARK: - 保存本地设置 
    public func saveSettingInfoToLocal(_ userInfoDic:NSDictionary)
    {
        saveInfoToLocal(info: userInfoDic, infoKey: USER_SETTING_INFO)
    }
    
    //MARK: - 返回本地设置
    public func userSettingInfoFromLocal() -> NSDictionary
    {
        let userInfoDic : NSDictionary = infoFromLocal(infoKey: USER_SETTING_INFO) as! NSDictionary
        return userInfoDic
    }
}
